import 'package:flutter/material.dart';
import 'package:osc/res/colors.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/utils/string_util.dart';

class CommonWithChildDialog extends Dialog {
  final String title;
  final String negativeText;
  final String positiveText;
  final Function onCloseEvent;
  final Function onPositivePressEvent;
  final Widget child;
  final Color positiveBgColor;
  final Color positiveTextColor;
  final Color negativeBgColor;
  final Color negativeTextColor;
  final Alignment alignment;

  CommonWithChildDialog({
    Key key,
    this.title,
    @required this.child,
    this.negativeText,
    this.positiveText,
    this.positiveBgColor = Colors.red,
    this.positiveTextColor = Colors.white,
    this.negativeBgColor = Colours.gray_cc,
    this.negativeTextColor = Colors.white,
    this.onPositivePressEvent,
    this.onCloseEvent,
    this.alignment = Alignment.centerLeft,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return new Padding(
      padding: EdgeInsets.all(Dimens.gap_activity_horizontal),
      child: new Material(
        type: MaterialType.transparency,
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Container(
              decoration: ShapeDecoration(
                color: Colors.white,
                shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(
                    Radius.circular(Dimens.gap_view_big),
                  ),
                ),
              ),
              margin: EdgeInsets.all(Dimens.gap_view_big),
              child: new Column(
                children: <Widget>[
                  new Padding(
                    padding: const EdgeInsets.all(Dimens.gap_view_normal),
                    child: new Stack(
                      alignment: AlignmentDirectional.centerEnd,
                      children: <Widget>[
                        new Center(
                          child: new Text(
                            StringUtil.isEmpty(title)?"":title,
                            style: new TextStyle(
                              fontSize: Dimens.font_sp18,
                            ),
                          ),
                        ),
                        new GestureDetector(
                          onTap: this.onCloseEvent != null?this.onCloseEvent:(){
                            Navigator.pop(context);
                          },
                          child: new Padding(
                            padding: EdgeInsets.all(Dimens.gap_view_small),
                            child: new Icon(
                              Icons.close,
                              color: Colors.black54,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  new Divider(
                    color: Colours.divider,
                    height: StringUtil.isEmpty(title)?0:Dimens.gap_divider_height_smallest,
                  ),
                  Padding(
                      padding: EdgeInsets.symmetric(horizontal: Dimens.gap_view_big, vertical: Dimens.gap_view_normal),
                    child: Align(
                      alignment: alignment,
                      child: child,
                    ),
                  ),
                  buildBottomButtonGroup(context),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  buildBottomButtonGroup(BuildContext context) {
    var widgets = <Widget>[];
    if (negativeText != null && negativeText.isNotEmpty) widgets.add(buildBottomCancelButton(context));
    if(!StringUtil.isEmpty(negativeText) && !StringUtil.isEmpty(positiveText)){
      widgets.add(SizedBox(width: Dimens.gap_view_biggest));
    }
    if (positiveText != null && positiveText.isNotEmpty) widgets.add(buildBottomPositiveButton(context));

    return Padding(
        padding: EdgeInsets.symmetric(horizontal: Dimens.gap_view_normal, vertical: Dimens.gap_view_normal),
      child: Flex(
        direction: Axis.horizontal,
        children: widgets,
      ),
    );
  }

  buildBottomCancelButton(BuildContext context) {
    return buildButton(context, negativeText, negativeBgColor, negativeTextColor, onCloseEvent);
  }

  buildBottomPositiveButton(BuildContext context) {
    return buildButton(context, positiveText, positiveBgColor, positiveTextColor, onPositivePressEvent);
  }

  buildButton(BuildContext context, String text, Color color, Color textColor, Function onPressed) {
    return new Flexible(
      fit: FlexFit.tight,
      child: new FlatButton(
        color: color,
        onPressed: onPressed != null?onPressed:(){
          Navigator.pop(context);
        },
        child: new Text(
          text,
          style: TextStyle(
            color: textColor,
            fontSize: Dimens.font_sp16,
          ),
        ),
      ),
    );
  }
}